/**
 * 商品列表  by  cata type
 * @author douzy  2016-07-14 add
 * @constructor
 */
function ShopProductList() {

    this.type = type;
    this.local = local;

    //url
    this.ajaxUrl = {
        parent: "/catalog/catalogListByParentAndType.do", //父节点
        childrent: "/catalog/catalogListByPid.do?pid={0}",//子节点
        product: "/product/getProductBycata/{0}.do"//产品
    }

    //变量
    this.variable = {
        $cataParent: $("#cataParent"),
        $cataParentTemp: $("#cataParent-template"),
        $productPannelTemplate: $("#productPannel-template"),
        $cataChildrentTmp: $("#cataChildrent-template"),
        $btnSecondary: $(".am-btn-secondary"),
        $tryOnClothes: $("#try-on-clothes"),
        $cataProductTemplate: $("#cataProduct-template"),
        $cataProductPannel: $("#cataProductPannel")

    }

    this.common = new common()   //通用

}
ShopProductList.prototype.init = function () {
    var self = this;
    self.loadParent();
}

//页面初始化选中父节点
ShopProductList.prototype.initCheck = function () {
    var self = this;
    self.variable.$cataParent.children("li").each(function (i) {
        var t = parseInt($(this).attr("t"));
        if (self.type == t) {
            $(this).addClass("am-active");
            self.chkParentIndex = parseInt(i + 1);
            self.loadChildrent(self.type);
        } else
            $(this).removeClass("am-active");
    });
}

//父节点事件绑定
ShopProductList.prototype.parentEvent = function () {
    var self = this;
    //
    $(".am-btn-secondary").click(function () {
        var pid = parseInt($(this).parent().attr("t"));
        self.type = pid;
        self.initCheck();
        //self.loadChildrent(pid);
        self.variable.$cataProductPannel.html("");
    });
}

//父节点加载
ShopProductList.prototype.loadParent = function () {
    var self = this;
    $.ajax({
        type: "GET",
        url: self.ajaxUrl.parent,
        data: {local: self.local},
        dataType: "json",
        success: function (data) {
            var $bannerObj = self.common.evalJson(data);
            if (!!$bannerObj) {
                if ($bannerObj.status == "success") {
                    var handleHelper = Handlebars.registerHelper("addOne", function (index) {
                        return index + 1;
                    });
                    var myTemplate = Handlebars.compile(self.variable.$cataParentTemp.html());
                    self.variable.$cataParent.html(myTemplate(data));

                    //生成父节点的同时, 生成与父节点数量相等的div容器
                    //var myTemplate1 = Handlebars.compile(self.variable.$productPannelTemplate.html());
                    //self.variable.$tryOnClothes.html(myTemplate1(data));

                    self.initCheck();
                    self.parentEvent();
                }
            }
        }
    });
}
//子节点加载
ShopProductList.prototype.loadChildrent = function (pid) {
    var self = this;
    if (!!pid) {
        $.ajax({
            type: "GET",
            url: self.ajaxUrl.childrent.format(pid),
            dataType: "json",
            success: function (data) {
                var $bannerObj = self.common.evalJson(data);
                if (!!$bannerObj && ($bannerObj.status == "success")) {
                    var myTemplate = Handlebars.compile(self.variable.$cataChildrentTmp.html());
                    $("#cataChildrent").html(myTemplate(data));
                    self.cataChildrentEvent();
                }
            }
        });
    }
}

//子类事件
ShopProductList.prototype.cataChildrentEvent = function () {
    var self = this;
    var $cataChildrent = $(".cataChildrent");
    var $cateChil = $("#cataChildrent .cataChildrent:eq(0)");
    $cataChildrent.click(function () {
        var cataId = parseInt($(this).attr("cataId"));
        $(this).addClass("am-badge-primary");
        $(this).siblings().removeClass("am-badge-primary");
        self.loadProduct(cataId);
    });
    $cateChil.click();
    $cateChil.addClass("am-badge-primary");


}

//商品加载
ShopProductList.prototype.loadProduct = function (cataId) {
    var self = this;
    if (!!cataId) {
        $.ajax({
            type: "GET",
            url: self.ajaxUrl.product.format(cataId),
            dataType: "json",
            success: function (data) {
                var $bannerObj = self.common.evalJson(data);
                if (!!$bannerObj) {
                    Handlebars.registerHelper("isXiezi", function (v1, options) {
                        if (v1 == "xiezi" || v1 == null || v1 == "") {
                            //满足添加继续执行
                            return options.fn(this);
                        } else {
                            //不满足条件执行{{else}}部分
                            return options.inverse(this);
                        }
                    });


                    var myTemplate = Handlebars.compile(self.variable.$cataProductTemplate.html());
                    self.variable.$cataProductPannel.html(myTemplate(data));
                }
            }
        });
    }
}


$(function () {
    new ShopProductList().init();
    $('#cataParent').on('click', 'li', function () {
        $(this).addClass('am-active');
    })
});